package adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a17376080_dlz_finaltest.BrowseCartoonsActivity;
import com.example.a17376080_dlz_finaltest.R;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import utils.HttpUtil;

public class MajorAdapter extends RecyclerView.Adapter<MajorAdapter.ViewHolder> {
    private Context context;
    private static final int SUCCESS = 1;
    private List<Comic> mcomicList;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                //加载网络成功进行UI的更新,处理得到的图片资源
                case SUCCESS:
                    Object[] objects= (Object[]) msg.obj;
                    //byte[] Picture = (byte[]) objects[1];
                    ImageView imageView= (ImageView) objects[0];
                    if(imageView.getTag().equals(objects[2])){
                        byte[] Picture = (byte[]) objects[1];
                        //使用BitmapFactory工厂，把字节数组转化为bitmap
                        Bitmap bitmap = BitmapFactory.decodeByteArray(Picture, 0, Picture.length);
                        //通过imageview，设置图片
                        imageView.setImageBitmap(bitmap);
                    }
                    break;
                    default:
                        break;


            }
        }
    };


    static class ViewHolder extends RecyclerView.ViewHolder{
       ImageView imageId;
        TextView sign,title,author,complete,several,fabulous;
        View comicView;
        public ViewHolder(View view){
            super(view);
            comicView=view;
            sign=view.findViewById(R.id.tv_sign);
            author=view.findViewById(R.id.tv_author);
            imageId=view.findViewById(R.id.iv_image);
            several=view.findViewById(R.id.tv_several);
        }
    }
    public MajorAdapter(List<Comic> comicList,Context context){
        this.mcomicList=comicList;
        this.context=context;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.major_item,parent,false);
        final ViewHolder holder=new ViewHolder(view);
        holder.imageId.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                int position=holder.getAdapterPosition();
                Comic comic=mcomicList.get(position);
               int comic_id=comic.getComic_id();
                Log.d("看一下Id", String.valueOf(comic_id));
                //点击图片查看漫画
                Intent intent=new Intent(v.getContext(),BrowseCartoonsActivity.class);
                intent.putExtra("position",comic_id);
                v.getContext().startActivity(intent);
                Toast.makeText(v.getContext(),"你点击leview"+comic.getSign(),Toast.LENGTH_LONG).show();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Comic comic=mcomicList.get(position);
    holder.sign.setText(comic.getSign());
    holder.author.setText(comic.getAuthor());
        holder.imageId.setImageDrawable(context.getDrawable(R.mipmap.timg));
        holder.imageId.setTag(comic.getImageId());

    sendImageViewHttp(comic.getImageId(),holder.imageId);
//    holder.imageId.setImageResource(comic.getImageId());
    holder.several.setText(comic.getSeveral());
    }

    private void sendImageViewHttp(final String imageId ,final ImageView imageView) {
        HttpUtil.sendOkHttpRequest(imageId, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("失败了", "onFailure: ");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                byte[] Picture_bt = response.body().bytes();
                //通过handler更新UI
               Object[] objects=new Object[]{imageView,Picture_bt,imageId};
                Message message=handler.obtainMessage();
                message.obj=objects;
                message.what = SUCCESS;
                handler.sendMessage(message);

            }
        });
    }


    @Override
    public int getItemCount() {
        return mcomicList.size();
    }
}
